import java.util.Stack;

public class Z {
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < s.length(); i++) {
                char ch=s.charAt(i);
                if(('(' == ch) || ('[' == ch) || ('{' == ch))
                {
                    stack.push(ch);
                }else{
                    char ch1=s.charAt(i);
                    if(stack.empty()){
                        return false;
                    }
                    char top = stack.peek();

                    // 检查是否匹配
                    if ((top == '(' && ch == ')') ||
                            (top == '[' && ch == ']') ||
                            (top == '{' && ch == '}')){
                        stack.pop();
                    }else{
                        return false;
                    }
                }
            }
            return stack.empty();
        }
    }
}
